﻿//----------------------------------------------------------------
//CodeArt (2007)
//
//Author: jianyi0115@163.com
//Created: 2010-9-13
//
//History:
// 
//----------------------------------------------------------------
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;

namespace CodeArt.SharePoint.CamlQuery
{
    public class LookupQueryField : TypedQueryField<int>
    {
        /// <summary>
        /// Lookup查询，直接按照ID比较
        /// </summary>
        /// <param name="name">internal name of field</param>
        public LookupQueryField(string name)
            : base(name)
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="name">internal or display name of field</param>
        /// <param name="isInternalName">if the first parameter is a internal name</param>
        public LookupQueryField(string name, bool isInternalName)
            : base(name, isInternalName)
        {
        }

        protected override CamlExpression CreateExpression(string op, object value)
        {
            SingleCamlExpression q = new SingleCamlExpression(this.FieldName, op, value, typeof(SPFieldLookupValue), this.IsInternalName);
            return q;
        }

        public override  CamlExpression In(params int[] values)
        {           
            return new InExpression(this.FieldName, this.IsInternalName,
                values.Select(x => x.ToString()).ToArray()
                , SPFieldType.Lookup.ToString());
        }

        const string Template_Includes_Text = @"<Includes> 
<FieldRef Name='{0}'/> 
<Value Type='Lookup'>{1}</Value> 
</Includes>";
        const string Template_Includes_ID = @"<Includes> 
<FieldRef Name='{0}' LookupId='TRUE'/> 
<Value Type='Lookup'>{1}</Value> 
</Includes>";

        const string Template_NotIncludes_Text = @"<NotIncludes> 
<FieldRef Name='{0}'/> 
<Value Type='Lookup'>{1}</Value> 
</NotIncludes>";

        const string Template_NotIncludes_ID = @"<NotIncludes> 
<FieldRef Name='{0}' LookupId='TRUE'/>
<Value Type='Lookup'>{1}</Value> 
</NotIncludes>";

        public CamlExpression Includes(string value)
        {
            return new TemplateCamlExpression(Template_Includes_Text,this.FieldName, this.IsInternalName, value);
        }

        public CamlExpression Includes(int id)
        {
            return new TemplateCamlExpression(Template_Includes_ID, this.FieldName, this.IsInternalName, id.ToString());
        }

        public CamlExpression NotIncludes(string value)
        {
            return new TemplateCamlExpression(Template_NotIncludes_Text, this.FieldName, this.IsInternalName, value);
        }

        public CamlExpression NotIncludes(int id)
        {
            return new TemplateCamlExpression(Template_NotIncludes_ID, this.FieldName, this.IsInternalName, id.ToString());
        }
    }
}
